home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / DISKS / Issue36 / Clinic / LotteryU.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1998-04-14  |  1.7 KB  |  81 lines

  1. unit LotteryU;
  2.  
  3. interface
  4.  
  5. uses
  6.   WinProcs, WinTypes, Messages, SysUtils, Classes, Graphics, Controls, Forms,
  7.   Dialogs, StdCtrls, ExtCtrls;
  8.  
  9. type
  10.   TfrmLottery = class(TForm)
  11.     lblNo1: TLabel;
  12.     lblNo2: TLabel;
  13.     lblNo3: TLabel;
  14.     lblNo4: TLabel;
  15.     lblNo5: TLabel;
  16.     lblNo6: TLabel;
  17.     procedure FormCreate(Sender: TObject);
  18.     procedure FormDestroy(Sender: TObject);
  19.     procedure GenericClick(Sender: TObject);
  20.   private
  21.     List: TList;
  22.   end;
  23.  
  24. var
  25.   frmLottery: TfrmLottery;
  26.  
  27. implementation
  28.  
  29. {$R *.DFM}
  30.  
  31. {$ifndef Win32}
  32. procedure Sleep(MSec: Integer);
  33. var
  34.   OldTime: TDateTime;
  35. begin
  36.   OldTime := Now;
  37.   repeat until Now >= OldTime + MSec / MSecsPerDay
  38. end;
  39. {$endif}
  40.  
  41. procedure TfrmLottery.FormCreate(Sender: TObject);
  42. begin
  43.   { Initialise random number generator }
  44.   Randomize;
  45.   { Create number list }
  46.   List := TList.Create
  47. end;
  48.  
  49. procedure TfrmLottery.FormDestroy(Sender: TObject);
  50. begin
  51.   { Dispose of number list }
  52.   List.Free;
  53.   List := nil
  54. end;
  55.  
  56. procedure TfrmLottery.GenericClick(Sender: TObject);
  57. var
  58.   Loop, Index: Integer;
  59. const
  60.   MaxNum = 49;
  61. begin
  62.   { Empty the list }
  63.   List.Clear;
  64.   { Fill the list with 49 numbers }
  65.   for Loop := 1 to MaxNum do
  66.     List.Add(Pointer(Loop));
  67.   { Loop for each number sought }
  68.   for Loop := 1 to 6 do
  69.   begin
  70.     { Choose one of the remaining numbers in the list }
  71.     Index := Random(List.Count);
  72.     { Write it in the appropriate label }
  73.     (FindComponent('lblNo' + IntToStr(Loop)) as TLabel).Caption :=
  74.       IntToStr(Longint(List[Index]));
  75.     { Remove the number from the list, so it won't be picked again }
  76.     List.Delete(Index);
  77.   end
  78. end;
  79.  
  80. end.
  81.